package com.newpage.module_classin.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.newpage.module_classin.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * author: zhouyh
 * created on: 2021/6/20 10:56 下午
 * description:
 */
public class ChoiceQuestionAdapter extends RecyclerView.Adapter<ChoiceQuestionAdapter.ChoiceQuestionViewHolder> {


    private Context context;
    private SparseArray<Boolean> map = new SparseArray<>();
    private List<String> answerOptions = new ArrayList<>();
    private boolean singleChoice = true;
    private int selectIndex = -1;

    public ChoiceQuestionAdapter(Context context,boolean choice) {
        this.context = context;
        this.singleChoice = choice;
    }

    public void setData(List<String> data){
        if (data!=null){
            answerOptions.clear();
            answerOptions.addAll(data);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @NotNull
    @Override
    public ChoiceQuestionViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.classin_choice_question_item,null);
        return new ChoiceQuestionViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ChoiceQuestionViewHolder holder, int position) {
        holder.tv_choice.setText(answerOptions.get(position));
        holder.tv_choice.setOnClickListener(v -> {
            if (listener!=null){
                listener.onClick(position);
                if (map.get(position) == null || !map.get(position)){
                    map.put(position,true);
                }else {
                    map.put(position,false);
                }
                setSelectIndex(position);
                notifyDataSetChanged();
            }
        });


        //如果是单选题
        if (singleChoice){
            if (getSelectIndex() == position){
                map.put(getSelectIndex(),map.get(position));
            }else {
                map.put(position,false);
            }
        }

        if (map.get(position)!=null && map.get(position)){
            holder.tv_choice.setSelected(true);
        }else {
            holder.tv_choice.setSelected(false);
        }



    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    @Override
    public int getItemCount() {
        return answerOptions.size();
    }

    class ChoiceQuestionViewHolder extends RecyclerView.ViewHolder {


        private TextView tv_choice;

        public ChoiceQuestionViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            tv_choice = itemView.findViewById(R.id.tv_choice_content);
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
